package org.yanghaokun.basic.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.yanghaokun.basic.mapper.BaseMapper;
import org.yanghaokun.basic.query.BaseQuery;
import org.yanghaokun.basic.service.IBaseService;
import org.yanghaokun.basic.util.PageList;

import java.util.List;

@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
//一般来说，一个业务类中查询的方法较多，所有类级别设置为只读事务。
public class BaseServiceImpl<T> implements IBaseService<T>{

    //一定要加上T，我们在使用的时候是没有BaseMapper动态代理类生成的，使用的都是Department等子类
    // 到时需要更加t找具体的
    @Autowired
    private BaseMapper<T> mapper;

    @Transactional
    @Override
    public void add(T t) {

        mapper.save(t);
    }
    @Transactional
    @Override
    public void del(Long id) {

        mapper.delete(id);
    }
    @Transactional
    @Override
    public void update(T t) {

        mapper.update(t);
    }

    @Override
    public T findOne(Long id) {
        System.out.println(mapper.getClass());
        return mapper.findOne(id);
    }

    @Override
    public List<T> findAll() {
        return mapper.findAll();
    }

    @Override
    public PageList<T> getList(BaseQuery query) {
        //查询总数
        Integer total = mapper.queryCount(query);
        if (total==0)
            return new PageList<>();

        List<T> rows = mapper.queryData(query);
        return new PageList<>(total,rows);
    }
}
